package cn.aitstone.basic.listener;

import cn.aitstone.order.domain.OrderPetAcquisition;
import cn.aitstone.order.mapper.OrderPetAcquisitionMapper;
import cn.aitstone.pet.domain.SearchMasterMsg;
import cn.aitstone.pet.mapper.SearchMasterMsgMapper;
import io.netty.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * rocketmq 消息监听，@RocketMQMessageListener中的selectorExpression为tag，默认为*
 */
@Slf4j
@Component
@RocketMQMessageListener(topic = "queue_test_topic",selectorExpression="*",consumerGroup = "queue_group_test")
public class RocketMQMsgListener implements RocketMQListener<MessageExt> {
    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Override
    public void onMessage(MessageExt message) {
        byte[] body = message.getBody();
        String msg = new String(body, CharsetUtil.UTF_8);
        log.info("接收到消息：{}", msg);
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.loadById(new Long(msg));
        if (orderPetAcquisition.getState() == 0){
            SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.loadById(orderPetAcquisition.getSearchMasterMsgId());
            searchMasterMsg.setShopId(null);
            searchMasterMsgMapper.update(searchMasterMsg);
            // 修改订单状态为取消
            orderPetAcquisition.setState(-1);
            orderPetAcquisitionMapper.update(orderPetAcquisition);


        }

    }

}